package com.tianqu.tms.www.finance.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.entity.TransferFinanceInfo;
import com.tianqu.tms.entity.TransferProcessHistory;
import com.tianqu.tms.www.finance.actionform.CarProxyPaySearchInputForm;

public class CarProxyPayTransInputController extends TmsController {
	private Logger logger = Logger.getLogger(CarProxyPayTransInputController.class);

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		try {
			controller.begin();
			CarProxyPaySearchInputForm inputForm = (CarProxyPaySearchInputForm) uc.getInputForm();
			TmsUserProfile profile = uc.getUserProfile();
			String serial = inputForm.getSerial();
			String price = inputForm.getRealAmount();
			String outAmount = inputForm.getOutAmount();
			TransferAcceptedInfo accept = new TransferAcceptedInfo();
			accept = accept.getAcceptInfoBySerial(serial,"");
			TransferFinanceInfo finance = new TransferFinanceInfo();
			finance = finance.findFinanceInfoByserial(serial);
			TransferProcessHistory process = new TransferProcessHistory();

			if ((accept.getStatus() == 3 ||accept.getStatus() == 0) && accept.getWaitForStatus() == 13) {
				finance.setAgencyPrice(Integer.parseInt(StringUtil.removeDH(price)));
				if (outAmount != null && !"".equals(outAmount)) {
					finance.setExtendedPrice(Integer.parseInt(StringUtil.removeDH(outAmount)));
				}else{
					finance.setAgencyChargeType(1);//待办收费类型为1 使用现金
				}
				process.setReason("");
				process.setSerial(serial);
				process.setBusinessType(27);
				process.setInsertUserId(profile.getUniqueId());
				process.insertHisInfoAtAssess(process);
				accept.setWaitForStatus(15);
				accept.setUpdateUserId(profile.getUniqueId());
				TransferAcceptedInfo accept1 = new TransferAcceptedInfo();
				accept1 = accept1.getAcceptInfoBySerial(serial,"");
				accept.setChangeTradeTicket(accept1.getChangeTradeTicket());
				accept.updateAcceptStatusBySerial(accept);
				
				finance.setSerial(serial);
				finance.updateFinanceInfoPrice(finance);	
				if (outAmount != null && !"".equals(outAmount)) 
				{
					TransferFinanceInfo ff = new TransferFinanceInfo();
					ff.setSerial(serial);
					ff.setExtendedPriceUserId(profile.getUniqueId());
					ff.updateExtendedPriceDate(ff);
				}else{
					TransferFinanceInfo ff = new TransferFinanceInfo();
					ff.setSerial(serial);
					ff.setAgencyPriceUserId(profile.getUniqueId());
					ff.updateAgencyPriceDate(ff);

				}
			}
			if (accept.getChangeTradeTicket() == 1) {
				
				process.setSerial(serial);
				process.setBusinessType(24);
				process.setReason(inputForm.getReason());
				process.setInsertUserId(profile.getUniqueId());
				process.insertHisInfoAtAssess(process);
				accept.setChangeTradeTicket(2);
				accept.setUpdateUserId(profile.getUniqueId());
				accept.updateAcceptStatusBySerial(accept);
				finance.setSerial(serial);
				finance.setChangePrice(Integer.parseInt(StringUtil.removeDH(price)));
				finance.updateFinanceInfoPrice(finance);	
				TransferFinanceInfo ff = new TransferFinanceInfo();
				ff.setSerial(serial);
				ff.setChangePriceUserId(profile.getUniqueId());
				ff.updateChangePriceDate(ff);
			}

			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 绯荤粺寮傚父
			if (e instanceof TmsException) {
				try {
					throw e;
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

		logger.debug(this.getClass().getName() + "#execute was completed.");

	}

}
